package or;

import CommonStructure.TreeNode;
//import sun.reflect.generics.tree.Tree;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class T0337 {
    Map<TreeNode, Integer> mapT, mapF;

    public int rob(TreeNode root) {
        mapT = new HashMap<>();
        mapF = new HashMap<>();
        return dp(root, false);
    }

    public int dp(TreeNode father, boolean boo) {
        if ((boo ? mapT : mapF).containsKey(father)) {
            return (boo ? mapT : mapF).get(father);
        }
        int sum = 0;
        if (boo) {
            if (father.left != null) sum += dp(father.left, false);
            if (father.right != null) sum += dp(father.right, false);
        } else {
            int a = 0, b = 0;
            a += father.val;
            if (father.left != null) {
                a += dp(father.left, true);
                b += dp(father.left, false);
            }
            if (father.right != null) {
                a += dp(father.right, true);
                b += dp(father.right, false);
            }
            sum = Math.max(a, b);
        }
        (boo ? mapT : mapF).put(father, sum);
        return sum;
    }
}
